<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Item_request extends CI_Controller {

	private $aprv= 'N';
	private $req = 'N';
	private $rls = 'N';

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		
		$result = $this->item_request_model->get_inv_access($this->session->userdata('user_id'));

		foreach ($result as $key) {
			$this->aprv = $key->APRV; $this->req = $key->REQ; $this->rls = $key->RLS;
		}
	}

	public function index(){

	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array(
	  		'req'		=>	$this->req ,
	  		'aprv'		=>	$this->aprv,
	  		'rls'		=>	$this->rls,
	  		'status'	=> 	$this->item_request_model->get_status()
	  	));
	  $this->load->view('layouts/footer');
	}
	public function add_form(){
			$this->load->view($this->session->userdata('foradd'),array(
				'items'		=>  $this->item_request_model->get_items(),
				'req_num'	=>	$this->item_request_model->get_reqNum('INV_REQUEST_NUM'),
				'users'		=> 	$this->item_request_model->get_inv_users($this->session->userdata('company_id'),'REQ'),
				'status'	=> 	$this->item_request_model->get_status()
				));
	}
	public function edit_form($id,$do){
			$req_data = $this->item_request_model->get_req_datus($id);
			$this->load->view($this->session->userdata('foredit'),array(
				'operation'	=> 	$do,
				'items'		=>  $this->item_request_model->get_items(),
				'datas'		=> 	$req_data,
				'req_id'	=> 	$req_data[0]->inv_request_id,
				'users'		=> 	$this->item_request_model->get_inv_users($this->session->userdata('company_id'),'REQ'),
				'status'	=> 	$this->item_request_model->get_status()
				));
		
	}
	
	public function view_form($id){
		$req_data = $this->item_request_model->get_req_datus($id);
			$this->load->view($this->session->userdata('forview'),array(
				'items'		=>  $this->item_request_model->get_items(),
				'datas'		=> 	$req_data,
				'req_id'	=> 	$req_data[0]->inv_request_id,
				'users'		=> 	$this->item_request_model->get_inv_users($this->session->userdata('company_id'),'REQ'),
				'status'	=> 	$this->item_request_model->get_status()
				));
	}

	public function insert_request(){
			$data = array(
				':p_request_num' 	=>	$this->input->post('request_num'),
				':p_trans_source'	=> 	'INV_REQUEST',
				':p_trans_src_id'	=>  'ITEM_REQUEST_SRC',
				':p_request_date' 	=>	$this->input->post('request_date'),
				':p_requested_by' 	=>	$this->input->post('request_by'),
				':p_approved_by' 	=>	$this->input->post('approve_by'),
				':p_request_status' =>	$this->input->post('request_stat'),
				':p_user_id' 		=>	$this->session->userdata('user_id')
			);

			$this->item_request_model->insert_req_data($data);
	}

	public function update_request(){
			$data = array(
				':p_request_id' 	=>	$this->input->post('h_reqedit_id'),
				':p_request_date' 	=>	$this->input->post('ed_reqDate'),
				':p_request_by' 	=>	$this->input->post('ed_reqBy'),
				':p_request_stat' 	=>	$this->input->post('ed_reqStat'),
				':p_user_id' 		=>	$this->session->userdata('user_id')
			);

			$this->item_request_model->update_req_data($data);
	}

	public function insert_req_content(){
		$data = array(
	 		':p_request_id'		=> $this->input->post('c_req_id'),
	 		':p_sequence_num'	=> $this->input->post('req_sequence'),
	 		':p_item_id'		=> $this->input->post('request_item'),
	 		':p_item_qty'		=> $this->input->post('request_qty'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$query= $this->item_request_model->insert_content($data);
	 	if($query[0]->error == 1){
			echo json_encode($query);
		}else{
			$this->view_req_contents($this->input->post('c_req_id'));
		}
	}

	public function insert_req_content_edit(){
		$data = array(
	 		':p_request_id'		=> $this->input->post('c_reqId_ed'),
	 		':p_sequence_num'	=> $this->input->post('ed_seq'),
	 		':p_item_id'		=> $this->input->post('ed_item'),
	 		':p_item_qty'		=> $this->input->post('ed_qty'),
	 		':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$query= $this->item_request_model->insert_content($data);
	 	
			echo json_encode($query);
		
	}

	public function view_req_contents($id){
		$query = $this->item_request_model->get_contents($id);
		echo json_encode($query);
	}

	public function searchRequest(){
 		$rn = $this->input->post('search_num');
		$rs = $this->input->post('search_stat');
		$query = $this->item_request_model->search_request($rn, $rs);

		if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}
 	}

 	public function approve_request(){
 		$request_id = $this->input->post('request_id');
 		$data = array(
 			':p_request_id'		=>	$this->input->post('request_id'),
 			':p_request_stat'	=>	$this->input->post('request_stat'),
 			':p_user_id'		=>	$this->session->userdata('user_id')
 		);
 		$this->item_request_model->approve_request($data);
 	}

 	public function delete_req_contents(){
 		$data = array(
	 		':p_line_id'		=> $this->input->post('line_id')
	 		);
	 	$query = $this->item_request_model->delete_req_contents($data);
		if($query[0]->error == 1){
			echo json_encode($query);
		}else{
			$this->view_req_contents($this->input->post('request_id'));
		}
 	}



	
	 
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */